import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 导入模块
import auth from './modules/auth'
import points from './modules/points'

const store = new Vuex.Store({
  state: {
    // 全局加载状态
    loading: false,
    // 网络状态
    networkStatus: true,
    // 系统信息
    systemInfo: {}
  },
  
  mutations: {
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_NETWORK_STATUS(state, status) {
      state.networkStatus = status
    },
    SET_SYSTEM_INFO(state, info) {
      state.systemInfo = info
    }
  },
  
  actions: {
    // 设置加载状态
    setLoading({ commit }, loading) {
      commit('SET_LOADING', loading)
    },
    
    // 获取系统信息
    getSystemInfo({ commit }) {
      return new Promise((resolve) => {
        uni.getSystemInfo({
          success: (res) => {
            commit('SET_SYSTEM_INFO', res)
            resolve(res)
          }
        })
      })
    }
  },
  
  modules: {
    auth,
    points,
    // user,
    // products,
    // orders,
    // files
  },
  
  plugins: [
    // 暂时注释掉持久化插件
    // createPersistedState({
    //   storage: {
    //     getItem: (key) => uni.getStorageSync(key),
    //     setItem: (key, value) => uni.setStorageSync(key, value),
    //     removeItem: (key) => uni.removeStorageSync(key)
    //   },
    //   paths: ['auth.token', 'auth.userInfo', 'user.profile']
    // })
  ]
})

export default store